package com.yunhoo.base.db.impl;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.yunhoo.base.model.Role;
import com.yunhoo.sys.db.util.IbatisTemplate;
import com.yunhoo.sys.util.common.LoginParam;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

public class RoleImpl {

    private static final String NAMESPACE = "role.";

    private static Map paramMap() {
        Map map = new HashMap();
        map.put("orgCode", LoginParam.getOrg().getCode());
        return map;
    }

    public static List<Role> allList() {
        Map map = paramMap();
        return IbatisTemplate.queryForList(NAMESPACE + "allList", map);
    }

    public static boolean insert(Role data) {

        SqlMapClient sqlmap = IbatisTemplate.getSqlMapClient();
        try {
            sqlmap.startTransaction();

            sqlmap.insert(NAMESPACE + "insert", data);

            sqlmap.commitTransaction();
        } catch (SQLException ex) {
            Logger.getLogger(RoleImpl.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        } finally {
            try {
                sqlmap.endTransaction();
            } catch (SQLException ex1) {
                Logger.getLogger(RoleImpl.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }
        return true;
    }

    public static boolean update(Role data) {

        SqlMapClient sqlmap = IbatisTemplate.getSqlMapClient();
        try {
            sqlmap.startTransaction();

            sqlmap.update(NAMESPACE + "update", data);

            sqlmap.commitTransaction();
        } catch (SQLException ex) {
            Logger.getLogger(RoleImpl.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        } finally {
            try {
                sqlmap.endTransaction();
            } catch (SQLException ex1) {
                Logger.getLogger(RoleImpl.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }
        return true;
    }

    public static boolean delete(String number) {

        SqlMapClient sqlmap = IbatisTemplate.getSqlMapClient();
        try {
            sqlmap.startTransaction();

            sqlmap.delete(NAMESPACE + "delete", number);

            sqlmap.commitTransaction();
        } catch (SQLException ex) {
            Logger.getLogger(RoleImpl.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        } finally {
            try {
                sqlmap.endTransaction();
            } catch (SQLException ex1) {
                Logger.getLogger(RoleImpl.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }
        return true;
    }

    public static void main(String[] args) {
        Map map = new HashMap();
        map.put("orgCode", "10201");
        List li = IbatisTemplate.queryForList(NAMESPACE + "allList", map);
        System.out.println(" * * * " + "li : " + li.size() + "  * * * ");
    }
}
